package com.frend.rpc.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.frend.rpc.model.DeploymentForm;
import org.frend.rpc.model.DeploymentModel;
import org.frend.rpc.model.ProcessDefinitionModel;
import org.frend.rpc.model.ProcessInstanceModel;
import org.frend.rpc.model.TaskModel;
import org.frend.rpc.service.DeploymentService;
import org.frend.rpc.service.ProcessDefinitionService;
import org.frend.rpc.service.StartProcessInstanceService;
import org.frend.rpc.service.TaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping(value="/activiti")
public class ProcessController {

    private static final Logger logger = LoggerFactory.getLogger(ProcessController.class);

    @Reference
    private DeploymentService deploymentService;

    @Reference
    private ProcessDefinitionService processDefinitionService;

    @Reference
    private StartProcessInstanceService startProcessInstanceService;

    @Reference
    private TaskService taskService;

    /**
     * 上传附件页面
     * @return
     */
    @RequestMapping(value="/toDeployment")
    public String toDeployment() {

        return "deployment";
    }

    /**
     * 上传附件
     * @param name
     * @param file
     * @return
     */
    @RequestMapping(value = "deployment" , method= RequestMethod.POST,produces="text/plain;charset=UTF-8")
    @ResponseBody
    public String deploymentProcessDefinition(String name,@RequestParam("file") MultipartFile file){

        String resource = file.getOriginalFilename();
        DeploymentForm form = new DeploymentForm();
        form.setName(name);
        form.setResource(resource);
        DeploymentModel deployment = deploymentService.deploymentProcessDefinition(
                name,resource
        );

        if(deployment.getId() != null){
            logger.info("添加成功");
            return "{\\\"isOk\\\":true}";
        }

        return "{\\\"isOk\\\":false}";
    }

    /**
     * 查看部署流程
     * @return
     */
    @RequestMapping(value = "queryCreateProcessDefinition" , method= RequestMethod.GET)
    public String queryCreateProcessDefinition(Model model){
        List<ProcessDefinitionModel>  list = processDefinitionService.queryCreateProcessDefinition();
        model.addAttribute("list",list);
        return "processDefinition";
    }

    /**
     * 启动流程
     * @param processDefinitionKey
     * @return
     */
    @RequestMapping(value = "startProcessInstanceService" , method= RequestMethod.GET)
    @ResponseBody
    public String startProcessInstanceService(String processDefinitionKey){
        ProcessInstanceModel model = startProcessInstanceService.startProcessInstanceByKey(processDefinitionKey);
        return "{\\\"isOk\\\":true}";
    }

    /**
     * 查看我的任务
     * @param model
     * @param assignee
     * @return
     */
    @RequestMapping(value = "getTaskAssignee" , method= RequestMethod.GET)
    public String getTaskAssignee(Model model,String assignee){
        List<TaskModel> list = taskService.getTaskAssignee(assignee);
        model.addAttribute("list",list);
        return "taskList";
    }

}
